package com.thinkgem.jeesite.common.listener.order;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.thinkgem.jeesite.modules.shop.service.order.ShopOrderService;

public class OrderListener implements ServletContextListener {
    private Timer timer = new Timer();
    public ServletContext servletContext;
    ShopOrderService shopOrderService = null;
    
    public void contextInitialized(ServletContextEvent sce) {
        this.servletContext = sce.getServletContext();
        
        try {
            // 第一个参数是要运行的代码，第二个参数是从什么时候开始运行，第三个参数是每隔多久在运行一次。
            sce.getServletContext().log("定时器已启动");
            // 设置在每晚24:00:00点执行任务
            Date nowDate = new Date();
            sce.getServletContext().log("nowDate:" + nowDate);
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, nowDate.getHours()); // 24 ,可以更改时间
            calendar.set(Calendar.MINUTE, nowDate.getMinutes()); // 00可以更改分数
            calendar.set(Calendar.SECOND, nowDate.getSeconds() + 10);// 00 //默认为0,不以秒计
            Date date = calendar.getTime();
            // 第一个参数 是要运行的代码，第二个参数是指定的时间
            timer.schedule(new SystemTask(sce), date, (60 * 1000 * 30));
            sce.getServletContext().log("订单检测已经添加任务调度表");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void contextDestroyed(ServletContextEvent sce) {
        try {
            timer.cancel();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
